﻿/*
 * Created by SharpDevelop.
 * User: IEscarro
 * Date: 9/1/2009
 * Time: 9:56 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using Buaya.Util;
using System.Reflection;

namespace Buaya.Model
{
	/// <summary>
	/// Description of User.
	/// </summary>
	public class User
	{
		int id;
		string answer;
		IList<UserModule> modules = new List<UserModule>();
		
		public virtual IList<UserModule> Modules {
			get { return modules; }
			set { modules = value; }
		}
		
		public virtual string Answer {
			get { return answer; }
			set { answer = value; }
		}
		int challengeQuestion;
		
		public virtual int ChallengeQuestion {
			get { return challengeQuestion; }
			set { challengeQuestion = value; }
		}
		
		public virtual int Id {
			get { return id; }
			set { id = value; }
		}
		string name;
		
		public virtual string Name {
			get { return name; }
			set { name = value; }
		}
		string password;
		
		public virtual string Password {
			get { return password; }
			set { password = value; }
		}
		
		public User()
		{
		}
		
		public virtual UserModule GetModule(Module module)
		{
			foreach (UserModule m in modules) {
				if (m.Module.Id == module.Id) {
					return m;
				}
			}
			return null;
		}
		
		public virtual void AddModule(Module m)
		{
			AddModule(new UserModule(m));
		}
		
		public virtual void AddModule(UserModule m)
		{
			m.User = this;
			modules.Add(m);
		}
		
		protected virtual void OnModuleAdded(EventArgs e)
		{
			if (ModuleAdded != null) {
				ModuleAdded(this, e);
			}
		}
		
		public virtual event EventHandler ModuleAdded;
	}
	
	public class UserModule
	{
		Module module;
		IList<UserModuleCommand> commands = new List<UserModuleCommand>();
		int order;
		
		public virtual int Order {
			get { return order; }
			set { order = value; }
		}
		
		public virtual IList<UserModuleCommand> Commands {
			get { return commands; }
			set { commands = value; }
		}
		
		public virtual Module Module {
			get { return module; }
			set { module = value; }
		}
		User user;
		
		public virtual User User {
			get { return user; }
			set { user = value; }
		}
		int id;
		
		public virtual int Id {
			get { return id; }
			set { id = value; }
		}
		
		public UserModule()
		{
		}
		
		public UserModule(Module module)
		{
			this.module  = module;
		}
		
		public virtual void AddCommand(UserModuleCommand command)
		{
			command.Module = this;
			commands.Add(command);
		}
		
		protected virtual void OnCommandAdded(EventArgs e)
		{
			if (CommandAdded != null) {
				CommandAdded(this, e);
			}
		}
		
		public virtual event EventHandler CommandAdded;
	}
	
	public class UserModuleCommand
	{
		UserModule module;
		ModuleCommand moduleCommand;
		
		public virtual ModuleCommand ModuleCommand {
			get { return moduleCommand; }
			set { moduleCommand = value; }
		}
		
		public virtual UserModule Module {
			get { return module; }
			set { module = value; }
		}
		int id;

		public virtual int Id {
			get { return id; }
			set { id = value; }
		}
		string command;
		
		[Obsolete()]
		public virtual string Command {
			get { return command; }
			set { command = value; }
		}
		string name;
		
		[Obsolete()]
		public virtual string Name {
			get { return name; }
			set { name = value; }
		}
		
		public UserModuleCommand()
		{
		}
		
		public UserModuleCommand(ModuleCommand command)
		{
			this.name = command.Name;
			this.command = command.Command;
		}
		
		public UserModuleCommand(string name, string command)
		{
			this.name = name;
			this.command = command;
		}
		
		public virtual ICommand CreateCommandInstance()
		{
			if (command != "") {
				Assembly a = Assembly.GetExecutingAssembly();
				return (ICommand)a.CreateInstance(command);
			} else {
				return null;
			}
		}
	}
}
